import 'package:f_enjoy_plus/utils/http.dart';
import 'package:get/get.dart';

import 'addOrEditVM.dart';

class MyRepairVM extends GetxController {
  static MyRepairVM get to => Get.find(); // 添加这一行

  var houseList = <HouseItemRes>[].obs;
  var repairTypeList = <repairTypeRes>[].obs;
  var repairList = <RepairPageRow>[].obs;

  /// 获取房屋列表
  Future<void> fetchHouseList() async {
    try {
      final response = await http.get('/house');
      print('response: $response');
      houseList.value =
          (response as List).map((e) => HouseItemRes.fromJson(e)).toList();
    } catch (e) {
      print('error: $e');
    }
  }

  /// 获取报修项目列表
  Future<void> fetchRepairTypeList() async {
    try {
      final response = await http.get('/repairItem');
      print('response: $response');
      repairTypeList.value =
          (response as List).map((e) => repairTypeRes.fromJson(e)).toList();
    } catch (e) {
      print('error: $e');
    }
  }

  Future<void> fetchMyRepairList() async {
    try {
      final params = RepairPageReq(current: '1', pageSize: '10');
      final response = await http.get('/repair', params: params.toJson());
      print('--->response: $response');
      repairList.value =
          (response['rows'] as List)
              .map((e) => RepairPageRow.fromJson(e))
              .toList();
    } catch (e) {
      print('error--->: $e');
    }
  }

  @override
void onInit() {
  super.onInit();
  fetchHouseList().then((_) {
    fetchMyRepairList();
  });
  fetchRepairTypeList();
}
}

class RepairPageReq {
  ///当前页码
  String current;

  ///每页大小
  String pageSize;

  RepairPageReq({required this.current, required this.pageSize});
  Map<String, dynamic> toJson() {
    return {'current': current, 'pageSize': pageSize};
  }
}

///返回数据
class RepairPageData {
  ///总页数
  double pageTotal;

  ///数据
  List<RepairPageRow> rows;

  ///总条数
  double total;

  RepairPageData({
    required this.pageTotal,
    required this.rows,
    required this.total,
  });
}

class RepairPageRow {
  ///预约日期
  String appointment;

  ///附件信息，0-9个
  List<String> attachment;

  ///问题描述-做多200字
  String description;

  ///房屋信息id
  String? houseId;

  ///主键id
  String id;

  ///手机号
  String mobile;

  ///维修项目id
  String? repairItemId;

  ///维修项目名称
  String repairItemName;

  ///维修状态码：1受理中2上门中3已完成0已取消
  double? status;

  RepairPageRow({
    required this.appointment,
    required this.attachment,
    required this.description,
    this.houseId,
    required this.id,
    required this.mobile,
    this.repairItemId,
    required this.repairItemName,
    this.status,
  });

  factory RepairPageRow.fromJson(Map<String, dynamic> json) {
    return RepairPageRow(
      appointment: json['appointment']?.toString() ?? '',
      attachment: (json['attachment'] as List?)?.map((e) => e.toString()).toList() ?? [],
      description: json['description']?.toString() ?? '',
      houseId: json['houseId']?.toString(),
      id: json['id']?.toString() ?? '',
      mobile: json['mobile']?.toString() ?? '',
      repairItemId: json['repairItemId']?.toString(),
      repairItemName: json['repairItemName']?.toString() ?? '',
      status: json['status'] is int ? (json['status'] as int).toDouble() : json['status'] as double?,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'appointment': appointment,
      'attachment': attachment,
      'description': description,
      'houseId': houseId,
      'id': id,
      'mobile': mobile,
      'repairItemId': repairItemId,
      'repairItemName': repairItemName,
    };
  }
}

class HouseItemRes {
  String id;
  String name;
  HouseItemRes({required this.id, required this.name});

  factory HouseItemRes.fromJson(Map<String, dynamic> json) {
    return HouseItemRes(
      id: json['id']?.toString() ?? '', 
      name: json['name']?.toString() ?? ''
    );
  }

  Map<String, dynamic> toJson() {
    return {'id': id, 'name': name};
  }
}

class repairTypeRes {
  String id;
  String name;
  repairTypeRes({required this.id, required this.name});

  factory repairTypeRes.fromJson(Map<String, dynamic> json) {
    return repairTypeRes(
      id: json['id']?.toString() ?? '', 
      name: json['name']?.toString() ?? ''
    );
  }

  Map<String, dynamic> toJson() {
    return {'id': id, 'name': name};
  }
}
